package com.example.redstarcommerce.cn.dh.user.controller;

import com.example.redstarcommerce.cn.dh.common.vo.Result;
import com.example.redstarcommerce.cn.dh.user.pojo.User;
import com.example.redstarcommerce.cn.dh.user.pojo.UserLogin;
import com.example.redstarcommerce.cn.dh.user.service.impl.UserServiceImpl;
import com.example.redstarcommerce.cn.dh.user.utils.JwtSecrity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays; // 导入 Arrays 类
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
@CrossOrigin
@Api(tags = "用户接口", description = "用户")
@Slf4j
public class UserController {

    @Autowired
    private UserServiceImpl userService;

    @Autowired
    private JwtSecrity jwtSecrity;

    @Autowired
    private UserDetailsService userDetailsService;

    @RequestMapping("/getall")
    @ApiOperation(value = "查询所有用户", notes = "loginUser", httpMethod = "GET")
    public Object getAll() {
        Result<List<User>> all = userService.getAll();
        log.info("getAll: {}", all);
        return all;
    }

    @RequestMapping("/userlogin")
    @ApiOperation(value = "用户登录", notes = "loginUser", httpMethod = "POST")
    public Object loginUser(@RequestBody UserLogin userLogin) {
        String name = userLogin.getUsername();
        log.info("loginUser: {}", name);
        String password = userLogin.getPassword();
        log.info("loginpassword: {}", password);

        Result<List<User>> byName = userService.getByName(name, password);

        // 检查 byName 和 byName.getData() 是否为 null
        if (byName != null && byName.getData() != null && !byName.getData().isEmpty()) {
            User user = byName.getData().get(0);
            UserDetails userDetails = new User(user.getName(), user.getPassword(), Arrays.asList()); // 使用 Arrays.asList()
            String token = jwtSecrity.generateToken(userDetails);
            log.info("loginUser: {}", token);
            Map<String,Object> data = new HashMap<>();
            data.put("token",token);
            return Result.success(data);
        } else {
            log.info("loginUser: 用户名或密码错误");
            return Result.fail("用户名或密码错误");
        }
    }

    @RequestMapping("/insertuser")
    @ApiOperation(value = "用户注册", notes = "insertUser", httpMethod = "POST")
    public Object insertUser(String name, String password) {
        Result<Boolean> userResult = userService.insertUser(name, password);
        log.info("insertUser: {}", userResult);
        return userResult;
    }

    @RequestMapping("/updateuser")
    @ApiOperation(value = "用户修改", notes = "updateUser", httpMethod = "POST")
    public Object updateUser(String uid, String name, String password) {
        Result<Boolean> userResult = userService.updateUser(uid, name, password);
        log.info("updateUser: {}", userResult);
        return userResult;
    }

    @RequestMapping("/deleteuser")
    @ApiOperation(value = "用户删除", notes = "deleteUser", httpMethod = "POST")
    public Object deleteUser(String uid) {
        Result<Boolean> userResult = userService.deleteUser(uid);
        log.info("deleteUser: {}", userResult);
        return userResult;
    }
}
